// app/controller/Orders.js
const Controller = require('egg').Controller;

function toInt(str) {
  if (typeof str === 'number') return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class OrderController extends Controller {
  // 获取xx列表
  async index() {
    const ctx = this.ctx;
    const query = {
      limit: toInt(ctx.query.limit),
      offset: toInt(ctx.query.offset),
    };
    ctx.body = await ctx.model.Order.findAll(query);
  }

  // 根据id获取xx信息
  async show() {
    const ctx = this.ctx;
    ctx.body = await ctx.model.Order.findByPk(toInt(ctx.params.id));
  }
  // 创建新xx
  async create() {
    const ctx = this.ctx;
    const { user_id, goods_id, total } = ctx.request.body;
    const obj = { user_id, goods_id, total };

    const Order = await ctx.model.Order.create(obj);


    // 1.商品完成购买后，系统通过注册表找出购买者的3个上级ID
    const User = await ctx.model.User.findByPk(toInt(user_id));
    if (!User.pid) return;
    const UserList = await ctx.helper.seekPid(User.pid, 3);
    console.log(UserList);

    // 2.通过支付的价格，分别算出3个上家获得的利润。
    const Goods = await ctx.model.Goods.findByPk(toInt(goods_id));
    const { gain, price } = Goods;
    // 通过支付商品的价格来获取商品的总佣金 100*10%=10
    const commission = (price * gain / 100);

    // 2.2 分别算出3个上家获得的利润。
    // 2.3  分配利润判断有几级分销人 按照分销人来分配佣金
    // 2.4 打钱给用户
    if (UserList.length === 1) {
      console.log('佣金按照 100% 分');
      const Bill = await ctx.model.Bill.findOne({ where: { user_id: UserList[0] } });
      await ctx.model.Bill.update({
        balance: Number(Bill.balance) + commission,
        price: '+' + commission,
      }, { where: { user_id: Bill.user_id } });

    } else if (UserList.length === 2) {
      console.log('佣金按照 30% 70% 分');
      const Bill1 = await ctx.model.Bill.findOne({ where: { user_id: UserList[0] } });
      await ctx.model.Bill.update({
        balance: Number(Bill1.balance) + commission * 70 / 100,
        price: '+' + commission * 70 / 100,
      }, { where: { user_id: Bill1.user_id } });

      const Bill2 = await ctx.model.Bill.findOne({ where: { user_id: UserList[1] } });
      await ctx.model.Bill.update({
        balance: Number(Bill2.balance) + commission * 30 / 100,
        price: '+' + commission * 30 / 100,
      }, { where: { user_id: Bill2.user_id } });

    } else {
      console.log('佣金按照 20% 30% 50% 分');
      const Bill1 = await ctx.model.Bill.findOne({ where: { user_id: UserList[0] } });
      await ctx.model.Bill.update({
        balance: Number(Bill1.balance) + commission * 50 / 100,
        price: '+' + commission * 50 / 100,
      }, { where: { user_id: Bill1.user_id } });

      const Bill2 = await ctx.model.Bill.findOne({ where: { user_id: UserList[1] } });
      await ctx.model.Bill.update({
        balance: Number(Bill2.balance) + commission * 30 / 100,
        price: '+' + commission * 30 / 100,
      }, { where: { user_id: Bill2.user_id } });

      const Bill3 = await ctx.model.Bill.findOne({ where: { user_id: UserList[2] } });
      await ctx.model.Bill.update({
        balance: Number(Bill3.balance) + commission * 20 / 100,
        price: '+' + commission * 20 / 100,
      }, { where: { user_id: Bill3.user_id } });
    }


    ctx.status = 201;
    ctx.body = Order;

  }
  // 更新xx
  async update() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const Order = await ctx.model.Order.findByPk(id);
    if (!Order) {
      ctx.status = 404;
      return;
    }

    const { user_id, goods_id, total } = ctx.request.body;
    await Order.update({ user_id, goods_id, total });
    ctx.body = Order;
  }

  // 删除用户根据id
  async destroy() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const Order = await ctx.model.Order.findByPk(id);
    if (!Order) {
      ctx.status = 404;
      return;
    }

    await Order.destroy();
    ctx.status = 200;
  }
}

module.exports = OrderController;
